package com.khyr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.khyr.dao.HisPatientDao;
import com.khyr.dao.HisPatientFileDao;
import com.khyr.entity.HisPatient;
import com.khyr.entity.HisPatientFile;
import com.khyr.entity.User;
import com.khyr.service.HisPatientService;
import com.khyr.vo.R;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: HisPatientService
 * @author: Zyl
 * @date: 2025/3/17 16:51
 * @Version: 1.0
 * @description:
 */
@Service
public class HisPatientServiceImpl implements HisPatientService {
    @Autowired
    private HisPatientDao hisPatientDao;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HisPatientFileDao hisPatientFileDao;

    @Override
    public R<HisPatient> getByName(String phone) {
        QueryWrapper<HisPatient> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        HisPatient hisPatient = hisPatientDao.selectOne(queryWrapper);
        if(hisPatient==null){
            return new R<>(500,"账号不存在",null);
        }
        return new R<HisPatient>(200,"登录成功",hisPatient);
    }

    @Override
    public R updatePatientInfo(HisPatient hisPatient) {
        //获取token
        String token = request.getHeader("token");
        //根据token获取手机号
        String phone = (String) redisTemplate.opsForHash().get("user::login::" + token, "name");
        //根据手机号获取用户名
        QueryWrapper<HisPatient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        int update = hisPatientDao.update(hisPatient, queryWrapper);
        if(update>0){
            return new R<>(200,"修改成功",hisPatient);
        }else{
            return new R<>(500,"修改失败",null);
        }

    }

    @Override
    public R<List<HisPatientFile>> getByPatientName() {
        String token = request.getHeader("token");
        Object phone = redisTemplate.opsForHash().get("user::login::" + token, "name");
        //通过手机号获取patient_id
        QueryWrapper<HisPatient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        HisPatient one = hisPatientDao.selectOne(queryWrapper);
        String patientId = one.getPatientId();
//        System.out.println("======================================= " + patientId);
//        QueryWrapper<HisPatientFile> queryWrapper2 = new QueryWrapper<>();
//        queryWrapper.eq("patient_id", patientId);
        List<HisPatientFile> hisPatientFiles = hisPatientFileDao.select(patientId);
        return new R<>(200,"查询成功",hisPatientFiles);
    }

    @Override
    public R<HisPatientFile> getPatientFileById(String patientId) {
        QueryWrapper<HisPatientFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patient_id", patientId);
        HisPatientFile one = hisPatientFileDao.selectOne(queryWrapper);
        return new R<>(200,"查询成功",one);
    }

    @Override
    public R updatePatientFile(HisPatientFile hisPatientFile) {
        QueryWrapper<HisPatientFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patient_id", hisPatientFile.getPatientId());
        int update = hisPatientFileDao.update(hisPatientFile, queryWrapper);
        if(update>0){
            return new R<>(200,"修改成功",null);
        }else{
            return new R<>(500,"修改失败",null);
        }
    }

    @Override
    public R deletePatientFile(String patientId) {
        int delete = hisPatientFileDao.deleteById(patientId);
        if(delete>0){
            return new R<>(200,"删除成功",null);
        }else{
            return new R<>(500,"删除失败",null);
        }
    }

    @Override
    public R addPatientFile(HisPatientFile patientFile) {
        // 通过手机号获取patient_id
        String token = request.getHeader("token");
        Object phone = redisTemplate.opsForHash().get("user::login::" + token, "name");
        QueryWrapper<HisPatient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        HisPatient one = hisPatientDao.selectOne(queryWrapper);
        patientFile.setPatientId(one.getPatientId());
        int insert = hisPatientFileDao.insert(patientFile);
        if(insert>0){
            return new R<>(200,"添加成功",null);
        }else{
            return new R<>(500,"添加失败",null);
        }
    }

}
